var tgd = tgd || {};
tgd.Utilities = tgd.Utilities || {};

tgd.Utilities.Throbber = function () {
	this._state = 0;
	this._running = false;
	this._canvas = document.createElement("div");
	this._canvas.className = "throbber";
	this._canvas.innerHTML = "|";
	this._clockwise = true;
	this._canvas.style.display = "none";

	if (tgd.Utilities.Throbber._initialized === undefined) {

		tgd.Utilities.Throbber.prototype._setNextState = function() {
			var nextState = "";

			//todo: there has to be a cleaner way to set the state for cw vs. ccw
			this._state += (this._clockwise ? 1 : -1);
			if (!this._clockwise) {				
				this._state += 4;
			}
			this._state %= 4;

			switch (this._state) {
				case 0: nextState = "|"; break;
				case 1: nextState = "/"; break;
				case 2: nextState = "&#x2014;"; break;
				case 3: nextState = "\\"; break;
				default: break;
			}

			this._canvas.innerHTML = nextState;
		}

		tgd.Utilities.Throbber.prototype.start = function() {
			if (!this._running) {
				this._running = true;
				var self = this;
				var throb = function() {
					self._setNextState();

					if (self._running) {
						setTimeout(throb, 150);
					} else {
						//???
					}
				};
				setTimeout(throb, 32);
			}
			this._canvas.style.display = "";
		}

		tgd.Utilities.Throbber.prototype.stop = function() {
			this._canvas.style.display = "none";
			this._running = false;
		}
		
		tgd.Utilities.Throbber.prototype.toggle = function() {
			this._clockwise = !this._clockwise;
		}

		tgd.Utilities.Throbber.prototype.getCanvas = function() {
			return this._canvas;
		}

		tgd.Utilities.Throbber._initialized = true;
	}
}